package net.pws.common.security.ehcache;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.pws.common.security.PrincipalManagerFactory;
import net.pws.common.security.spi.Principal;
import net.pws.common.security.spi.PrincipalManager;
import net.pws.common.security.spi.SecurityCacheException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;


public class DefaultPrincipalManager implements
                                    PrincipalManager,
                                    InitializingBean {
    
    private static final Log logger = LogFactory.getLog(DefaultPrincipalManager.class);
    
    private Ehcache ehcache;
    
    public Ehcache getEhcache() {
        return ehcache;
    }
    
    public void setEhcache(Ehcache ehcache) {
        this.ehcache = ehcache;
    }
    
    public void afterPropertiesSet() throws Exception {
        PrincipalManagerFactory.setPrincipalManager(this);
    }
    
    public Principal get(String key) {
        try {
            if (logger.isDebugEnabled()) {
                logger.debug("key: " + key);
            }
            if (key == null) {
                return null;
            }
            else {
                Element element = ehcache.get(key);
                if (element == null) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Element for " + key + " is null");
                    }
                    return null;
                }
                else {
                    return (Principal) element.getObjectValue();
                }
            }
        }
        catch (net.sf.ehcache.CacheException e) {
            throw new SecurityCacheException(e);
        }
    }
    
    public void put(String key, Principal value) {
        try {
            Element element = new Element(key, value);
            ehcache.put(element);
        }
        catch (IllegalArgumentException e) {
            throw new SecurityCacheException(e);
        }
        catch (IllegalStateException e) {
            throw new SecurityCacheException(e);
        }
        catch (net.sf.ehcache.CacheException e) {
            throw new SecurityCacheException(e);
        }
        
    }
    
    public void remove(String key) {
        try {
            ehcache.remove(key);
        }
        catch (ClassCastException e) {
            throw new SecurityCacheException(e);
        }
        catch (IllegalStateException e) {
            throw new SecurityCacheException(e);
        }
        catch (net.sf.ehcache.CacheException e) {
            throw new SecurityCacheException(e);
        }
    }
    
    public void clear() {
        try {
            ehcache.removeAll();
        }
        catch (IllegalStateException e) {
            throw new SecurityCacheException(e);
        }
        catch (net.sf.ehcache.CacheException e) {
            throw new SecurityCacheException(e);
        }
    }
}
